package com.example.baidumap_a.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import com.example.baidumap_a.R;
import com.example.baidumap_a.entity.Ohuang;

public class indexActivity extends AppCompatActivity {
    private String name, password;
    private static final int SI_SUCCESS = 0, SI_FAIL = 1, SI_ERROR = 2, SI_OTHER = 3;

    final OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
            Intent intent=new Intent(this,SignInActivity.class);
            startActivity(intent);
            finish();
        }

    }


    private void init() throws IOException {
        name = Ohuang.readfile(this, "name.txt");
        password = Ohuang.readfile(this, "password.txt");//读取保存的 用户名和密码
        if (name.isEmpty() || password.isEmpty()) {//如果没有用户名或密码的数据
            Intent intent = new Intent(this, SignInActivity.class);//进入登录Activity
            startActivity(intent);
            finish();
        } else {
            PostHttpRequest();//登录
        }
    }
//登录
    private void PostHttpRequest() {
        RequestBody formBody = new FormBody.Builder()
                .add("username", name)
                .add("password", password)
                .build();

        final Request request = new Request.Builder()
                .url("https://d30n971042.imdo.co/user/login")
                .post(formBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myHandler.sendEmptyMessage(SI_ERROR);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ResponseBody body = response.body();
                if (body != null) {
                    String code = body.string();
                    if ("success".equals(code)) {
                        myHandler.sendEmptyMessage(SI_SUCCESS);
                    } else if ("error".equals(code)) {
                        myHandler.sendEmptyMessage(SI_FAIL);
                    } else {
                        myHandler.sendEmptyMessage(SI_OTHER);
                    }
                    body.close();
                }
            }
        });
    }


    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SI_SUCCESS:

                    Intent intent = new Intent(indexActivity.this, MainActivity.class);
                    intent.putExtra("username", name);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    Intent intent1 = new Intent(indexActivity.this, SignInActivity.class);
                    startActivity(intent1);
                    finish();
                    break;

            }
        }
    };
}
